local lihui = fk.CreateSkill{
  name = "hanqing__lihui",
}

Fk:loadTranslationTable{
  ["hanqing__lihui"] = "砺喙",
  [":hanqing__lihui"] = "当你受到或造成伤害时，你可以失去1点体力或弃置两张牌。若如此做，此回合结束时，你选择X次："..
  "1.回复1点体力；2.摸两张牌；背水：依次明置、弃置共计三张手牌（X为本回合此前发动〖砺喙〗的次数）。",

  ["#hanqing__lihui-invoke"] = "砺喙：弃置两张牌，或直接点“确定”失去1点体力，本回合结束时执行效果",
  ["hanqing__lihui_beishui"] = "背水：明置、弃置共计三张手牌",
  ["#hanqing__wuhe-show"] = "砺喙：请明置至多三张手牌",
}

local DIY = require "packages/diy_utility/diy_utility"

local spec = {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(lihui.name) and
      (player.hp > 0 or #player:getCardIds("he") > 1)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = "#hanqing__lihui_active",
      prompt = "#hanqing__lihui-invoke",
    })
    if success and dat then
      event:setCostData(self, {cards = dat.cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    if #cards > 0 then
      room:throwCard(cards, lihui.name, player, player)
    else
      room:loseHp(player, 1, lihui.name)
    end
  end,
}
lihui:addEffect(fk.DamageCaused, spec)
lihui:addEffect(fk.DamageInflicted, spec)

lihui:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(lihui.name) and player:usedSkillTimes(lihui.name, Player.HistoryTurn) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local times = player:usedSkillTimes(lihui.name, Player.HistoryTurn) - 1
    for _ = 1, times do
      if player.dead then return end
      local choice = room:askToChoice(player, {
        choices = { "recover", "draw2", "hanqing__lihui_beishui" },
        skill_name = lihui.name,
      })
      if choice ~= "draw2" then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = lihui.name,
        }
        if player.dead then return end
      end
      if choice ~= "recover" then
        player:drawCards(2, lihui.name)
        if player.dead then return end
      end
      if choice == "hanqing__lihui_beishui" then
        if table.find(player:getCardIds("h"), function (id)
          return not table.contains(DIY.getShownCards(player), id)
        end) then
          local cards = DIY.askToShowCards(player, {
            min_num = 1,
            max_num = 3,
            skill_name = lihui.name,
            prompt = "#hanqing__wuhe-show",
            cancelable = true,
          })
          if player.dead then return end
          local n = 3 - #cards
          if n > 0 then
            room:askToDiscard(player, {
              min_num = n,
              max_num = n,
              include_equip = false,
              skill_name = lihui.name,
              cancelable = false,
            })
          end
        else
          room:askToDiscard(player, {
            min_num = 3,
            max_num = 3,
            include_equip = false,
            skill_name = lihui.name,
            cancelable = false,
          })
        end
      end
    end
  end,
})

return lihui
